/*
 *    Copyright 2009-2024 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package io.lvdaxian.mybatis19.logging;

import io.lvdaxian.mybatis19.logging.commons.JakartaCommonsLoggingImpl;
import io.lvdaxian.mybatis19.logging.nologging.NoLoggingImpl;
import io.lvdaxian.mybatis19.logging.stdout.StdOutImpl;

import java.lang.reflect.Constructor;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 构建 logging 的日志
 *
 * @author lihh
 */
public final class LogFactory {
  
  /**
   * Marker to be used by logging implementations that support markers.
   */
  public static final String MARKER = "MYBATIS";
  
  private static final ReentrantLock lock = new ReentrantLock();
  private static Constructor<? extends Log> logConstructor;
  
  static {
    tryImplementation(LogFactory::useCommonsLogging);
    tryImplementation(LogFactory::useNoLogging);
  }
  
  private LogFactory() {
    // disable construction
  }
  
  public static Log getLog(Class<?> clazz) {
    return getLog(clazz.getName());
  }
  
  public static Log getLog(String logger) {
    try {
      // 带有参数的 log 构造方法
      return logConstructor.newInstance(logger);
    } catch (Throwable t) {
      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
    }
  }
  
  public static void useCustomLogging(Class<? extends Log> clazz) {
    setImplementation(clazz);
  }
  
  public static void useCommonsLogging() {
    setImplementation(JakartaCommonsLoggingImpl.class);
  }
  
  public static void useStdOutLogging() {
    setImplementation(StdOutImpl.class);
  }
  
  public static void useNoLogging() {
    setImplementation(NoLoggingImpl.class);
  }
  
  private static void tryImplementation(Runnable runnable) {
    if (logConstructor == null) {
      try {
        runnable.run();
      } catch (Throwable t) {
        // ignore
      }
    }
  }
  
  private static void setImplementation(Class<? extends Log> implClass) {
    lock.lock();
    try {
      // 这里 拿到 参数为 String.class 的构造方法
      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
      // 从 这里创建实例
      Log log = candidate.newInstance(LogFactory.class.getName());
      if (log.isDebugEnabled()) {
        log.debug("Logging initialized using '" + implClass + "' adapter.");
      }
      // 这个就是 生成 的实例
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    } finally {
      lock.unlock();
    }
  }
  
}
